Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI sends multipart content even though API specifies application/json #4981

Closed
dblock opened this issue Oct 30, 2018 · 4 comments
Closed

UI sends multipart content even though API specifies application/json #4981

dblock opened this issue Oct 30, 2018 · 4 comments

Comments

@dblock
Copy link
Contributor

dblock commented Oct 30, 2018

Coming from artsy/doppler#142 with swagger-ui version: 3.19.4.

API specification is https://api.artsy.net/api/docs, which has

produces: [
"application/hal+json",
"application/json"
],

and all APIs say consumes with application/json.

The "Try me" UI POSTs multipart data while specifying JSON in content type, resulting in an error from our API.


  "type": "other_error",
  "message": "\nProblem:\n  message body does not match declared format\nResolution:\n  when specifying application/json as content-type, you must pass valid application/json in the request's 'body' ",

The curl code is incorrect too.

curl -X POST "http://localhost:3000/api/applications" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Access-Token: ..." -d "name=test"

Reading the code ... is this because my fields have in: "formData" instead of in: "body"?

That seems still weird. If the content type required is JSON, shouldn't everything be JSON?

@shockey
Copy link
Contributor

shockey commented Oct 30, 2018

Hey @dblock!

It looks like your definition is describing all of your parameters as in: formData, which is why you're seeing url encoded values.

If you load your definition into Swagger Editor, you'll see a couple dozen errors that look like this:

Semantic error: Operations with Parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property.


Your POST /api/applications parameter currently looks like this:

      parameters:
        - in: formData
          name: name
          description: Application name.
          type: string
          required: true

If you want JSON, you should change in: formData to in: body to avoid url encoding, change type: string to type: object, and use properties to describe the JSON object you'd like to send. You'll also have to move type and properties under schema, since you're describing a body parameter.

For example:

      parameters:
        - in: body
          name: name
          description: Application name.
          required: true
          schema:
            type: object
            properties:
              name:
                type: string

This will give you a curl output that looks like this:

curl -X POST "http://api.artsy.net/api/applications" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"name\": \"string\"}"

Hope this helps!

@dblock
Copy link
Contributor Author

dblock commented Oct 30, 2018

Thanks. This is pretty clear. Now I have to figure out why we're defaulting primitive types into formData in grape-swagger, ruby-grape/grape-swagger#721

@dblock dblock closed this as completed Oct 30, 2018
@shockey
Copy link
Contributor

shockey commented Oct 30, 2018

@dblock glad I could help! 😄

Note that I suggested using type: object. IMO, describing JSON as type: string is an antipattern (even though it's technically allowed) since you're missing out on a lot of the tooling ergonomics around object types and JSON.

@dblock
Copy link
Contributor Author

dblock commented Oct 30, 2018

Right. I meant each field is a string, but the whole thing is an object. When monkey patched as in ruby-grape/grape-swagger#721 I get this and everything works.

screen shot 2018-10-30 at 4 31 30 pm

@lock lock bot locked and limited conversation to collaborators Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants