Skip to content

Panic when attempting to update a Flex instance configuration due to an incorrect media type in the request #49

Open
twilio/twilio-go
#84
@RJPearson94

Description

@RJPearson94

Issue Summary

Hi, I have noticed that you cannot currently update a Twilio Flex instance configuration using the Twilio Go SDK.
Currently on every POST request the Content-Type header is hardcoded to be application/x-www-form-urlencoded as seen here
This is suitable for the majority of POST requests to the various Twilio API however to update the Flex Configuration, JSON needs to be sent in the request body.

To fix this I believe it will require changes in the Go SDK with the request handler and client being modified. Changes will also be required inside of the API mustache template in this repo.

I was thinking that inside the request handler in the Go SDK, the current Post function would be updated to create a new map that combines the Content-Type header (with the value of application/x-www-form-urlencoded) with the header map which is passed into the function. I was going with this approach to prevent mutating the headers map but if you want the header can either be passed in as another argument to SendRequest or the value could be appended to the headers map.
The current logic to set the Content-Type header inside of send request would be deleted. Another function PostJson would be added which would accept the path, body (of type map[string]interface{}) and the headers. This function would set the value of the Content-Type header to application/json.

In the generator, the API mustache template file will be updated to check if the request media type is application/json then the initialization of the data map (url.Values) and population of the params data into the map will be replaced with the params struct being marshalled into a map of type map[string]interface{} using the build-in Marshal function in the json package. Instead of the Post function being called on the request handler the PostJson function will be called.
If the request media type is not supplied or is application/x-www-form-urlencoded then the current implementation will be used, as per the current implementation.

The JSON and YML Open API templates don't currently include the request body schema which would be used to generate the input/ params struct, so the changes outlined above will resolve the issue highlighted however the Flex configuration cannot be updated using the SDK until the definition has been updated.
As this is an issue with the Open API templates do you want the issue for this raised on the twilio-oai repo?

This implementation should be flexible enough to be replicated to add support for creating a Serverless Asset Version and creating a Serverless Function Version via the SDK, both of which require form data to be supplied.

If you are happy with the changes I am happy to raise a PR for this.

Steps to Reproduce

  1. Copy the code below
  2. Execute the code using go run .go

Code Snippet

package main

import (
  "log"
  "os"

  "github.com/twilio/twilio-go"
)

func main() {
  accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
  authToken := os.Getenv("TWILIO_AUTH_TOKEN")
  client := twilio.NewRestClient(accountSid, authToken)

  resp, err := client.FlexV1.UpdateConfiguration()

  if err != nil {
    log.Panic(err.Error())
  }

  log.Printf("SID: %s", StringValue(resp.FlexServiceInstanceSid))
}

// StringValue is a utility method for obtaining the string value of a pointer
func StringValue(value *string) string {
  return *value
}

Exception/Log

2021/05/26 00:06:04 Status: 415 - ApiError 20415: The server does not support the media type transmitted in the request. (null) More info: https://www.twilio.com/docs/errors/20415
panic: Status: 415 - ApiError 20415: The server does not support the media type transmitted in the request. (null) More info: https://www.twilio.com/docs/errors/20415

goroutine 1 [running]:
log.Panic(0xc000151f68, 0x1, 0x1)
        /usr/local/opt/go/libexec/src/log/log.go:351 +0xae
main.main()
        /Users/RJPearson94/Documents/Code/Twilio/go-sdk/flex/configuration/flex_configuration.go:18 +0x18b
exit status 2

Technical details:

  • twilio-go version: v0.8.0
  • go version: go1.16.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions