1

I am attempting to do a POST request which I have got working through Postman but I am getting a 400 error when done from code.

I am trying to reach the POST endpoint from here: http://developer.oanda.com/rest-live-v20/order-ep/

Here is my code, does anything look incorrect or have I missed anything?

public void MakeOrder(string UID)
    {
        string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
        string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("Authorization", "Bearer 11699873cb44ea6260ca3aa42d2898ac-2896712134c5924a25af3525d3bea9b0");
            client.Headers.Add("Content-Type", "application/json");
            client.UploadString(url, body);
        }
    }

I'm very new to coding so apologies if it is something very simple.

Joe Brown
  • 67
  • 5

2 Answers2

0

use HttpClient from System.Net.Http:

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

using (var httpClient = new HttpClient())
{

       string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
       string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
       var content = new StringContent(body , Encoding.UTF8, "application/json");
       var result = httpClient.PostAsync(url, content).Result;
       var contents = result.Content.ReadAsStringAsync();
}
  • Unfortunately even with the Authorization header added im still getting a 400 error :/ Does string body look correct to you? – Joe Brown Jun 15 '20 at 18:46
  • have you tried your json with " instead of ' ? sometimes little specification like this can has effects . don't forget you need to escape " in string as \" – Mahyar Pasarzangene Jun 15 '20 at 19:32
  • Just tried it and unfortunately I still get the same error. On the OANDA Api instruction it says the following parameter is needed in the Header - Accept-Datetime-Format, I did not have to input anything like that into Postman to get it to work, just the Authorization and application/json. Could it be anything to do with that do you think? – Joe Brown Jun 16 '20 at 06:35
0

I would suggest you to use HttpClient over WebClient. You can find here the difference.

using (var httpClient = new HttpClient())
{

    string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
    string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
    var content = new StringContent(body, Encoding.UTF8, "application/json");
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTIzMDY0NTQsImlzcyI6IlRlc3QuY29tIiwiYXVkIjoiVGVzdC5jb20ifQ.c-3boD5NtOEhXNUnzPHGD4rY1lbEd-pjfn7C6kDPbxw");
    var result = httpClient.PostAsync(url, content).Result;
    var contents = result.Content.ReadAsStringAsync();
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197