1

When I try to post a string to my web api, the value is null. I have tried wrapping it in quotes, but it is still null.

AngularJS code:

return $http.post("http://localhost:59437/api/Recaptcha/Post", 
                                    vcRecaptchaService.getResponse());

Web Api code:

[EnableCors("*", "*", "*")]
public class RecaptchaController : ApiController
{
    public string Post([FromBody] string response)
    {
        return response;
    }
}

I also am not sure how this even works because, i don't have a response in my form body. vcRecaptchaService.getResponse() just returns a response string and then I am going to send that to google's verify api to verify the recaptcha, so the [FromBody] part doesn't make sense to me if that is not part of the body

2
  • what vcRecaptchaService.getResponse() method does? Commented Jul 21, 2015 at 20:14
  • @PankajParkar - It returns a string response when you check the recaptcha checbox. Commented Jul 21, 2015 at 20:18

2 Answers 2

1

Your post call should be sending data in json format like {response: 'something'}

  return $http.post("http://localhost:59437/api/Recaptcha/Post", 
       { response: vcRecaptchaService.getResponse() } //data 
  );
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this and I also tried doing { response: "'" + vcRecaptchaService.getResponse() + "'"}, but it is still null
@xaisoft no need of single quotes, are you getting any error in console?
Just checked, no console error. I tried hard-coding a string instead of using vcRecaptchaService.getResponse(), but that didn't work either.
When I look at chrome's developer tools, I see the Request Payload set to the string, but when it hits the Api Controller action, it is null.
0

Wrapping it in quotes worked, but I had to do it just like this:

return $http.post("http://localhost:59437/api/Recaptcha/Post",
           '"' + vcRecaptchaService.getResponse() + '"'
        );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.