go-swagger: POST client request gets a 415 response

Problem statement

When I send a POST request using the client API, I get a 415 (Unsupported Media Type) response. I noticed that the request has an extra header that sets “Transfer-Encoding: chunked”. On the other hand, sending the POST request via curl works fine because it doesn’t have that header.

This is my first go-swagger client and server. So, it is likely that I am doing something wrong, either in the code or in the swagger specification file.

Here is the request and response:

POST /api/v1/keys/k?value=v HTTP/1.1
Host: localhost:8001
User-Agent: Go-http-client/1.1
Transfer-Encoding: chunked
Accept: application/json
Accept-Encoding: gzip

0


HTTP/1.1 415 Unsupported Media Type
Connection: close
Content-Length: 113
Content-Type: application/json
Date: Mon, 23 Jan 2017 17:39:22 GMT

{"code":415,"message":"unsupported media type \"application/octet-stream\", only [application/json] are allowed"}
Error: unknown error (status 415): {resp:0xc4202ac120} 

And here is the error client is receiving:

&runtime.APIError{OperationName:"unknown error", Response:client.response{resp:(*http.Response)(0xc420090870)}, Code:415}

Swagger specification

swagger: '2.0'
info:
  version: "0.0.0"
  title: Testman
schemes:
  - http
host: localhost
basePath: /api/v1
produces:
  - application/json
consumes:
  - application/json
paths:
  /keys/{key}:
    get:
      parameters:
        - name: key
          in: path
          required: true
          type: string
      responses:
        200:
          description: Successful response
        404:
          description: Not found
    post:
      parameters:
        - name: key
          in: path
          required: true
          type: string
        - name: value
          in: query
          required: true
          type: string
      responses:
        200:
          description: Successful response

Steps to reproduce

I pushed everything needed to reproduce this issue at https://github.com/siadat/swagger-eg

Environment

Swagger version: 2.0 Go version: go version devel +6a3c6c0 Sat Jan 14 05:57:07 2017 +0000 linux/amd64 OS: linux

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

The content type of your request needs to be set to application/json At the moment it’s not present and then the default is application/octet-stream

Thanks for finding this, I’ll look into what needs to happen to work with go 1.8 over the weekend unless somebody gets to it earlier

it’s fixed in the runtime, just update your vendor or the package in your gopath