What is “406-Not Acceptable Response” in HTTP?

后端 未结 9 1749
礼貌的吻别
礼貌的吻别 2020-11-22 08:20

In my Ruby on Rails application I tried to upload an image through the POSTMAN REST client in Base64 format. When I POST the image I am getting a 406 Not Acceptable Resp

相关标签:
9条回答
  • 2020-11-22 08:39

    You mentioned you're using Ruby on Rails as a backend. You didn't post the code for the relevant method, but my guess is that it looks something like this:

    def create
      post = Post.create params[:post]
      respond_to do |format|
        format.json { render :json => post }
      end
    end
    

    Change it to:

    def create
      post = Post.create params[:post])
      render :json => post
    end
    

    And it will solve your problem. It worked for me :)

    0 讨论(0)
  • 2020-11-22 08:40

    In my case, I added:

    Content-Type: application/x-www-form-urlencoded
    

    solved my problem completely.

    0 讨论(0)
  • 2020-11-22 08:45
    const request = require('request');
    
    const headers = {
        'Accept': '*/*',
        'User-Agent': 'request',
    };
    
    const options = {
        url: "https://example.com/users/6",
        headers:  headers
    };
    
    request.get(options, (error, response, body) => {
        console.log(response.body);
    });
    
    0 讨论(0)
  • 2020-11-22 08:46

    In my case for a API in .NET-Core, the api is set to work with XML (by default is set to response with JSON), so I add this annotation in my Controller :

    [Produces("application/xml")]
    public class MyController : ControllerBase {...}
    

    Thank you for putting me on the path !

    0 讨论(0)
  • 2020-11-22 08:51

    You can also receive a 406 response when invalid cookies are stored or referenced in the browser - for example, when running a Rails server in Dev mode locally.

    If you happened to run two different projects on the same port, the browser might reference a cookie from a different localhost session.

    This has happened to me...tripped me up for a minute. Looking in browser > Developer Mode > Network showed it.

    0 讨论(0)
  • 2020-11-22 08:51

    If you are using 'request.js' you might use the following:

    var options = {
      url: 'localhost',
      method: 'GET',
      headers:{
        Accept: '*/*'
      }
    }
    
    request(options, function (error, response, body) {
      ...
    })
    
    0 讨论(0)
提交回复
热议问题