3

I have question about sending string value on axios post. I will provide code below for better understanding.

API CALL C# MVC .netcore

 public async Task<IActionResult> Save(string Code, [FromBody]string notes)
        {
           ------
        }

so above it will get the data of string from FROMBODY

REACT FRONT END USING AXIOS CALL

export const sendNote = async (Url, code, value) => {
  let Config = {
    headers: {
      "Content-Type": "application/json",
    },
  };
  try {
    const { data } = await axios.post(
      Url + code + "/Notes", 
     value,
      Config
    );
    return { data: data };
  } catch (error) {
    return { data: error.message };
  }
};

So as you can see on the code above, I am trying to send the value which is a string to the api call on C# but I get many different error like 400 or 415. I researched it on google and they mostly put it on the json format.

So end it like "TEST" instead of {data: "TEST"}

Thank you

7
  • Since you want to send a string why do you specify json content Content-Type: application/json This should be more like Content-Type: application/text. Commented Apr 7, 2021 at 16:56
  • @AntonKovachev Hi, thank you for your response and yes you are right I already tried using "application/text" but it still returns same error. When I run the api call separately it works. Well it still return error code 415.... Commented Apr 7, 2021 at 17:06
  • Perhaps you can leave Content-Type: application/json and send a request body similar to this one { notes: value } And also inspect with the web developer tools what data exactly you are sending Commented Apr 7, 2021 at 17:28
  • @AntonKovachev Yes, I thought about sending it as json object like what you mentioned above, but api call created by backend developer here told me to send as string only so That is the reason why I am looking into sending the data as string. Commented Apr 7, 2021 at 18:01
  • @SamuelJungHwanPark did you found a solution? Commented Jun 11, 2021 at 20:05

2 Answers 2

5

I found a solution that works for me. The problem is in Content-Type header.

...
headers: { "Content-Type": "application/json-patch+json" },
data: JSON.stringify(data),
params,
...

I have the same controller's method. I checked which headers uses in Swagger. It started working only after these changes.

Sign up to request clarification or add additional context in comments.

Comments

-1

Ok, an axios call like this:

    var object = {
                    myInt: 123456,
                    myString: "ciao Mondo"
                }

   return await axios.post("/Test/testFromBody", object)
           

want a controller method in C# like this:

[HttpPost, Route("testFromBody")]
public async Task<ActionResult> testFromBody([FromBody] TestClass test)
{
    return Ok();
}

where TestClass is specular to the object which we sent.

If we do an axios call like this:

var formPayload = new FormData();
formPayload.append("myInt", 134)
formPayload.append("myString", 'Ciao mondo')
return await axios.post("/Test/testFromForm", formPayload)

the controller looks like:

[HttpPost, Route("testFromForm")]
public async Task<ActionResult> testFromForm([FromForm] int myInt, string myString)
{
    return Ok();
}

otherwhise

var foo = "ciao Mondo"
var bar = 2345

return await axios.post(`/Test/testFromQueryParemtri?myInt=${foo}&myString=${bar}`)

the C# method

    [HttpPost, Route("testFromQueryParemtri")]
    public async Task<ActionResult> testFromQueryParemtri([FromQuery] int myInt, string myString)
{
    return Ok();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.