415 Unsupported Media Type asp.net core

Detail

I am trying to post a file from Postman to the endpoint I have created. but it gives me this error. I am not passing the header Content-Type in postman

415 Unsupported Media Type

API

[Consumes("multipart/form-data")]
[HttpPost]
public async Task<IActionResult> SendEmail([FromBody]Entity entity)
{
    try
    {

        return OK();
    }
    catch (Exception e)
    {
        throw e;
    }
}

Class

public class Entity 
{
    public List<IFormFile> Files { get; set; }
}
Score: 17
Tags: c#, asp.net-core-2.0, asp.net-core-webapi
Views: 21481
Date posted: 3/12/2018

Spring MVC 415 Unsupported Media Type

I am using Spring 3.2 and try to use an ajax post request to submit an array of json objects. If this is relevant, I escaped all special characters.

I am getting an HTTP Status of 415.

My controller is:

@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json")
    public @ResponseBody String saveProfileJson(@RequestBody String[] profileCheckedValues){
        System.out.println(profileCheckedValues.length);
        return "success";
    }

jquery is:

jQuery("#save").click(function () {
        var profileCheckedValues = [];
        jQuery.each(jQuery(".jsonCheck:checked"), function () {
            profileCheckedValues.push($(this).val());
        });
        if (profileCheckedValues.length != 0) {
            jQuery("body").addClass("loading");
            jQuery.ajax({
                type: "POST",
                contentType: "application/json",
                url: contextPath + "/sample/save-profile",
                data: "profileCheckedValues="+escape(profileCheckedValues),
                dataType: 'json',
                timeout: 600000,
                success: function (data) {
                    jQuery('body').removeClass("loading");
                },
                error: function (e) {
                    console.log("ERROR: ", e);
                    jQuery('body').removeClass("loading");
                }
            });
        }
    });

and an example of one of the objects from the array I am posting is the following json:

{
  "id": "534213341",
  "name": "Jack Lindamood",
  "first_name": "Jack",
  "last_name": "Lindamood",
  "link": "https://www.facebook.com/jack",
  "username": "jack",
  "gender": "male",
  "locale": "en_US",
  "updated_time": "2013-07-23T21:13:23+0000"
}

The error is:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method

Why is this error happening - does anyone know?

Score: 14
Tags: jquery, ajax, spring, spring-mvc
Views: 59539
Date posted: 8/7/2013

Spring MVC: @ResponseBody, 415 Unsupported Media Type

I'm having trouble mapping a JSON Post to a particular Java Object to save it via Hibernate

Headers of the Ajax call are correctly set...

Accept          application/json
Content-Type    application/json; charset=UTF-8

and the HTTP-Method is POST

Here comes my configuration...

My Spring MVC function mapping looks like this

@RequestMapping(value = {"/save.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj,Model model) {
   ...
}

in my container xml I have a ContentNegotiatingViewResolver like this

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html" />
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/assets/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="prefixJson" value="false" />
                <property name="objectMapper" ref="jacksonObjectMapper" />
            </bean>
        </list>
    </property>
</bean>

in my container xml the part for jackson

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer"
    init-method="init">

    <property name="messageConverters">
        <list>
            <bean id="marshallingHttpMessageConverter"
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
          >
          <property name="objectMapper" ref="jacksonObjectMapper" />
          </bean>
        </list>
    </property>
</bean> 

<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" /> 

I have

  • jackson-core-asl-1.8.5
  • jackson-mapper-asl-1.8.5

in my /lib folder

and Jackson works for a simple case like this

public class Simple {
  private int id;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }   
}

@RequestMapping(value = {"/saveSimple.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Simple simple,Model model) {
   ...
}

when I test it with curl

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1}'  http://<URL>/saveSimple.json

No problems, but if I test it with the Hibernate object I get the message

415 Unsupported Media Type

any ideas.

Score: 6
Tags: hibernate, spring-mvc, jackson
Views: 19039
Date posted: 8/22/2011

JMeter POST returns 415 - Unsupported Media Type

Well it's one step forward and two steps back for me. Thanks to you guys, I finally got the GET for my webservice working in JMeter. Now I'm trying a POST, and once again I'm not getting anywhere.

I'm using a HTTP Request and made the POST body as small as possible: <the_tag></the_tag>.
I even took the xml out and just used text.
No matter what's in the body, I get the 415 error.
What am I missing?
My implementation is HC3, protocol https, and I don't have anything else selected (other than the POST, of course). I also set the Content-Type to text/xml.

P.S. I should add that this POST works fine in SoapUI, so I don't think it's a server issue.

Score: 6
Tags: rest, jmeter
Views: 19186
Date posted: 1/23/2013

415 (Unsupported Media Type) in $http.post method

I'm quite new to REST and AngularJS, but after several hours of googling I couldn't find any answer to my question:

I'm trying to do a POST request from my angularjs frontend to my backend implemented in java (using JPA).

When I'm trying to create a json-object and to do a POST I always get the 415 (Unsupported Media Type) error.

(Actually I don't even get "into" the scope of the service (i.E. "IN SERVICE" doesn't get printed to the console).. If I add postData.toJSON(), it actually gets "POSTed", but arrives null ...

how do I have to format my 'postData' in Order to succesfully get POSTed?

(I also tried to write the Date-properties without ' " ' - no luck...)

Thank you for your help!


FrontEnd:

app.controller('WorkController', function($scope, $http) {

$scope.saveWork = function () {

    var postData = {
    "status" : "OPEN",
    "startDate": "1338364250000",
    "endDate": "1336364253400",
    "WorkText" : "Test"
    };


    $http.post("http://localhost:8080/service/v1/saveWork", postData)
        .success(function(data, status, headers, config){
            console.log("IN SAVE WORK - SUCCESS");
            console.log(status);
        })
        .error(function(){
            console.log("ERROR IN SAVE WORK!");
        })
}

});

Service:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response save(WorkDto wo){
            System.out.println("IN SERVICE");
    if(ass == null){
        System.out.println("Could nor persist work- null");
        return Response.noContent().build();
    } else{
        Work workDao = WorkTransformator.transform(wo);
        workDao.persist();
        return Response.ok().build();
    }
}
Score: 5
Tags: angularjs, post, jpa, http-status-code-415
Views: 48454
Date posted: 4/3/2014


2


© 2021 Search Overflow
Contributions licensed under cc by-sa