Last post Apr 02, 2019 01:07 AM by Ackerly Xu
Mar 25, 2019 07:41 PM|khabibulla|LINK
I'm new to web development and I'm trying to take an application I've developed for the desktop using .NET and create similar functionality in a web environment. What I am currently stuck on is getting past an "Error 405" when I run the following bit of
code to retrieve JSON from a web API:
fetch('https://<web site here>/api/v1/contract/getresponse?request.number=D2000-024',
headers: new Headers(
'apikey': '<api-key here>'
.then(response => response.json())
.catch(error => console.error(error))
If I comment out the headers section, I get what I expect (and error 401), but I am not sure why adding the header is causing the 405 error. In my desktop app I have no problem using the apikey on the same url.
If there is a better way to do this, please let me know.
Mar 26, 2019 01:21 AM|Ackerly Xu|LINK
405 means method not allowed , this usually happens when you try to make a cross-origin request.
Maybe the website where you send request is different from the website you send request to.
For example , your website is www.client.com and the website you want to send request to is www.server.com.
And the protocol also matters , if your website is http and the website you want to send request is https.
Port also matters.
For more information , you could refer to mdn
To solve your problem , if you have control over your server side , you could try to add the following code in your web.config.
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="*"/>
And in your global
protected void Application_EndRequest(object sender, EventArgs e)
if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
HttpContext.Current.Response.StatusCode = 200;
Apr 01, 2019 01:55 PM|khabibulla|LINK
Thanks for responding. I'm still trying to figure this out. I do not have control on the server side. I have no problems using the same URL within a Windows Forms application, retrieving the JSON data with the same URL and apikey. I'm only running into
My .NET code that is working looks like this:
private void btnGo_Click(object sender, EventArgs e)
this.Cursor = Cursors.WaitCursor;
WebClient client = new WebClient();
client.Headers["apikey"] = "<apikey here>";
string url = "https://<url here>/api/v1/contract/getresponse?request.number=" + txtContract.Text;
Stream stream = client.OpenRead(url);
StreamReader reader = new StreamReader(stream);
JObject jObject = JObject.Parse(reader.ReadLine());
txtContractDescription.Text = jObject["results"]["description"].ToString();
catch (Exception ex)
this.Cursor = Cursors.Default;
Apr 01, 2019 04:57 PM|mgebhard|LINK
I recommend that you write server side code to do the API call as the api-key is openly exposed to everyone that uses the application. Another benefit is you can reuse known working code plus you are not doing a cross site request.
Apr 02, 2019 01:07 AM|Ackerly Xu|LINK
If you want the same effect with your winform app, you should also call webclient at your c# server side.
Please create a asmx or webapi and in the asmx or webapi call your api with webclient and then returns the data to your client side.