0

I have a web api method:

    [HttpPost]
    public IActionResult Post([FromBody]string entryInJson)
    { ....  }    

This method is hit by a remote domain, so CORS is needed. As i wasn't successful in finding the relevant cs file in my mvc6 app, i used this solution in web.config:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>

When I do the post, using online tools like http://requestmaker.com/, i am getting this:

Request Headers Sent:

POST /api/Entries/ HTTP/1.1
Host: justalk.tukuoro.com
Accept: */*
Content-Type: text/plain
Content-Length: 179

Response Headers:

Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Expires: -1
Location: https://justalk.tukuoro.com/Account/Login?ReturnUrl=%2FHome%2FError
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Mon, 16 May 2016 05:25:53 GMT

Now, it seems like it is trying to redirect me (302) but to the error page, and it is not sending anything back from the post method, which returns:

return new ObjectResult(true);

I am not sure what is going on:

  1. Am I getting to the right controller? i have code that is supposed to log the incoming string but it does not create the log.
  2. If it is not working? why? is the response header telling me something I don't understand?
  3. Am I doing it right?

thank you for your time!

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
tarkil
  • 164
  • 1
  • 16
  • if you want send json request this "entryInJson" must be object type not string – Nazmul Hasan May 16 '16 at 05:51
  • @NazmulHasan when i test with a string it is still the same. so will changing it to object and sending JSON will work? or is the problem somewhere else? – tarkil May 16 '16 at 06:00
  • yes it will work if you changing it to object . http://stackoverflow.com/questions/15341744/posting-data-to-asp-net-web-api – Nazmul Hasan May 16 '16 at 06:03
  • can you show me your json format data? – Nazmul Hasan May 16 '16 at 06:10
  • i want to send a string and analyse it on the server. it doesnt matter the content of the string for a test purpose - still i am getting the same result from the post, and i am not sure it even gets to the controller. how can i be sure? – tarkil May 16 '16 at 06:20
  • could you share request data? – Nazmul Hasan May 16 '16 at 06:21
  • i edited the question to remove "JSON' from it, same question with string :) – tarkil May 16 '16 at 06:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112022/discussion-between-nazmul-hasan-and-tarkil). – Nazmul Hasan May 16 '16 at 06:22
  • i am not sure what do you mean, but this is the controller url: https://justalk.tukuoro.com/api/Entries/ and you can try sending any string. – tarkil May 16 '16 at 06:23

1 Answers1

0

if you want send json post request this entryInJson must be object type not string

for example

 [HttpPost]
    public IActionResult Post([FromBody]EntryInJson entryInJson)
    { ....  } 

chat comment

the code doing the http request to my post method - how does it convert this class "RemoteEntry" to JSON data

http://www.newtonsoft.com/json/help/html/serializingjson.htm

Update :

302 is not an error but a redircet to a tempoaray location. you could resend the request to the new location received in the response header.
and it's propably a server misconfiguration issue then.

modSecurity firewall blocking your requests an making automatics redirections (302).

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • if i want to send a string, and update the content type to text/plain, why is it not working? the result is the same. – tarkil May 16 '16 at 06:18