Asked  7 Months ago    Answers:  5   Viewed   46 times

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 Response. When I checked my database, the image was there and was successfully saved.

What is the reason for this error, is there anything I need to specify in my header?

My request:

URL --- http://localhost:3000/exercises.json

Header:

Content-Type  -  application/json

Raw data:

{
    "exercise": {
        "subbodypart_ids": [
            "1",
            "2"
        ],
        "name": "Exercise14"
    },
    "image_file_name": "Pressurebar Above.jpg",
    "image":"******base64 Format*******"
}

 Answers

88

Your operation did not fail.

Your backend service is saying that the response type it is returning is not provided in the Accept HTTP header in your Client request.

Ref: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

  1. Find out the response (content type) returned by Service.
  2. Provide this (content type) in your request Accept header.

http://en.wikipedia.org/wiki/HTTP_status_code -> 406

Tuesday, June 1, 2021
 
Alix
answered 7 Months ago
79

Aargh. Just switching it to GET sorted the issue. I'm not sure why on earth I was using POST for retrieval.

Still not sure why the issue didn't happen on my local wamp server, some difference in the apache settings somewhere.

Saturday, May 29, 2021
 
McAn
answered 7 Months ago
100

As of this time, the latest draft of the HTTPbis specification, which is intended to replace and make RFC 2616 obsolete, states:

The 400 (Bad Request) status code indicates that the server cannot or will not process the request because the received syntax is invalid, nonsensical, or exceeds some limitation on what the server is willing to process.

This definition, while of course still subject to change, ratifies the widely used practice of responding to logical errors with a 400.

Monday, June 28, 2021
 
Nickool
answered 6 Months ago
29

Turns out the answer to this one was pretty simple, but not covered well by any documentation I could find.

I was trying to implement an OData 3 endpoint on WebAPI 2.2. I was following several different tutorials, some for OData 3 and some for OData 4.

I was using OData 4 (System.Web.OData) in my WebApiConfig and OData 3 (System.Web.Http.OData) in my controller. Turns out, they don't play well together.

I decided to post the answer here in case anyone else has a similar issue.

To add a little value, and since I was mixing both anyway, I decided to go ahead and setup support for both version 3 and 4 by aliasing the namespaces in my WebApiConfig and then creating versioned controllers.

WebApiConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using ODataV3 = System.Web.Http.OData;
using ODataV4 = System.Web.OData;

namespace MyProject
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // OData V3 Route

            ODataV3.Builder.ODataModelBuilder builder3 = new ODataV3.Builder.ODataConventionModelBuilder();

            builder3.EntitySet<MarvelCharacter>("MarvelCharactersV3");
            // The MapODataRoute function is deprecated in WebAPI 2.2,
            // but I haven't found an alternative for supporting OData 3.
            config.Routes.MapODataRoute(
                routeName: "Marvel3",
                routePrefix: "dude3",
                model: builder3.GetEdmModel());

            // ODate V4 Route

            ODataV4.Builder.ODataModelBuilder builder4 = new ODataV4.Builder.ODataConventionModelBuilder();

            builder4.EntitySet<MarvelCharacter>("MarvelCharactersV4");
            ODataV4.Extensions.HttpConfigurationExtensions.MapODataServiceRoute(
                configuration: config,
                routeName: "Marvel4",
                routePrefix: "dude4",
                model: builder4.GetEdmModel());
        }
    }
}

MarvelCharactersV3 (OData 3):

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData; // OData V3
using System.Web.Http.OData.Query;
using Microsoft.Data.OData;
using MyProject;

namespace MyProject.Controllers
{
    public class MarvelCharactersV3Controller : ODataController
    {
        private static ODataValidationSettings _validationSettings = new ODataValidationSettings();

        // GET: odata/MarvelCharacters
        public IHttpActionResult GetMarvelCharactersV3(ODataQueryOptions<MarvelCharacter> queryOptions)
        {
            // validate the query.
            try
            {
                queryOptions.Validate(_validationSettings);
            }
            catch (ODataException ex)
            {
                return BadRequest(ex.Message);
            }

            var entities = new myEntities();
            var marvelCharacters = (from c in entities.MarvelCharacters select c).ToList();

            return Ok<IEnumerable<MarvelCharacter>>(marvelCharacters);
        }
    }
}

MarvelCharactersV4 (OData 4):

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData; // OData 4
using System.Web.OData.Query;
using Microsoft.Data.OData;
using MyProject;

namespace MyProject.Controllers
{
    public class MarvelCharactersV4Controller : ODataController
    {
        private static ODataValidationSettings _validationSettings = new ODataValidationSettings();

        // GET: odata/MarvelCharacters
        public IHttpActionResult GetMarvelCharactersV4(ODataQueryOptions<MarvelCharacter> queryOptions)
        {
            // validate the query.
            try
            {
                queryOptions.Validate(_validationSettings);
            }
            catch (ODataException ex)
            {
                return BadRequest(ex.Message);
            }

            var entities = new myEntities();
            var marvelCharacters = (from c in entities.MarvelCharacters select c).ToList();

            return Ok<IEnumerable<MarvelCharacter>>(marvelCharacters);
        }
    }
}

It's probably not the best architecture (I will probably create a library to consolidate similar code between the controllers), but I've tested and I can successfully query via OData 3 and OData 4, so I'm happy enough with it for now.

Sunday, October 10, 2021
 
pop
answered 2 Months ago
pop
63

I believe it should be a 404 status, since the request was valid, however no customerID was found.

Thursday, November 11, 2021
 
Johnson
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share