7
votes

I am using gatling for load performance testing on a brand new API. It seems fairly easy and well documented but I am facing an issue as simple as POST a request with Content-Type set to 'application/vnd.api+json' on the Header. All works well when doing the GET stuff but when launching a POST test I get a

HTTP response:
status=
415 Unsupported Media Type
headers= 
cache-control: [no-cache]
Content-Type: [application/vnd.api+json; charset=utf-8]
Date: [Fri, 08 Sep 2017 12:57:10 GMT]
Server: [nginx]
Vary: [Origin]
x-content-type-options: [nosniff]
x-frame-options: [SAMEORIGIN]
X-Request-Id: [ff993645-8e01-4689-82a8-2f0920e4f2a9]
x-runtime: [0.040662]
x-xss-protection: [1; mode=block]
Content-Length: [218]
Connection: [keep-alive]

body=
{"errors":[{"title":"Unsupported media type","detail":"All requests that create or update must use the 'application/vnd.api+json' Content-Type. This request specified 'application/json'.","code":"415","status":"415"}]}

Here is the scala code I am using for the http request:

object PostTokenGcm {
 val token = exec {
  http("TestAPI POST /tokens")
    .post("/tokens")
    .headers(Map("Authorization" -> testApiToken,
       "Content-Type" -> "application/vnd.api+json",
        "Accept" -> "application/vnd.api+json" ))
    .body(StringBody(gcmTokenRequestBody)).asJSON
    .check(status.is(201))
    .check(bodyString.exists)
}}

It seems that it is not setting the Content-Type?

Thank you for any lead!

1
Is it possible it is an error in the API? If you do the same request with a tool like postman does the API accept it?pedromss
I used Postman @pedromss and I get the desired 201 Created, no problems with the Content-Type set to application/vnd.api+jsonSofia

1 Answers

11
votes

In your POST definition you're using asJSON. According to notes in documentation about request headers:

http("foo").get("bar").asJSON is equivalent to:

http("foo").get("bar")
  .header(HttpHeaderNames.ContentType, HttpHeaderValues.ApplicationJson)
  .header(HttpHeaderNames.Accept, HttpHeaderValues.ApplicationJson)

... so, headers set in:

.headers(Map("Authorization" -> testApiToken,
       "Content-Type" -> "application/vnd.api+json",
        "Accept" -> "application/vnd.api+json" ))

... get overwritten by asJSON to "application/json" (which is the value of HttpHeaderValues.ApplicationJson).