1

how to post string from angular 7 to asp.net web API core 2.1

from angular i am trying below code

getDecTokenDetails(token: string): Observable<string> 
{
    //return this.http.get<String>("/api/MicroApp/manju");
    let headers = new HttpHeaders();
    headers.set('Content-Type', 'application/json');
    debugger;
    return this.http.post<string>("/api/MicroApp", token, { headers: headers 
    });
  }

IN webapi controller below code i am using

[HttpPost]
        public IActionResult Post([FromBody] string value)
        {
            return Ok("toekn received " + value);

        }

below error i am getting

HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "http://localhost:3974/api/MicroApp", ok: false, …}

Please help.

2 Answers 2

1

For bad request, it is caused that you did you set Content-Type header correctly. For let headers = new HttpHeaders(); headers.set('Content-Type', 'application/json');, headers is immutable object. Invoking class methods will return a new instance as result.

And for passing string, you need to use "" like

getDecTokenDetails(token: string): Observable<string> {
    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json');
    return this.http.post<string>("/api/SampleData/TT", `"${token}"`, {
    headers: headers
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ManjunathaT Check the network tab in web browser and share us the request
0

Try to do like this to see if it work

return this.http.post<string>("/api/MicroApp", { value: token }, { headers: headers });

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.