NetSuite REST Webservice C# RestSharp - Request failed with status code BadRequest

545 Views Asked by At

I continue to bang my head against the wall on this call. It returns perfectly fine in postman, but when I run through VS I get the following:

Exception Message: One or more errors occurred. (Request failed with status code BadRequest).

Exception Data: System.Collections.ListDictionaryInternal

I am trying to use the latest RestSharp build, but might have to downgrade to get it working so I can just use the Postman code provided. I've ensured to get a valid token before running the script as well.

*Note that the code builds fine in VS.

using System;
using RestSharp;
using System.Net.Http;
using System.Threading.Tasks;

namespace NetSuite
{

    class Program
    {

        static void Main(string[] args)
        {

            try
            {

                MainAsync().Wait();

                static async Task MainAsync()
                {

                    var client = new RestClient("https://xxxxxxxxxxxxxxxxxxx.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql?limit=500");
                    var request = new RestRequest();

                    request.AddHeader("Content-Type", "application/json");
                    request.AddHeader("prefer", "transient");
                    request.AddHeader("Authorization", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");


                    var body = @"{""q"": ""SELECT * FROM entity where id = 999""}";

                    request.AddParameter("application/json", body,  ParameterType.RequestBody);

                    var response = await client.PostAsync(request);

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR -- Exception Message: " + ex.Message.ToString());
                Console.WriteLine("ERROR -- Exception Data: " + ex.Data.ToString());
                System.Threading.Thread.Sleep(300);
            }

        }

    }
}
1

There are 1 best solutions below

0
On

You're missing the authentication code. Here is a sample using your suiteql request. I have found that RestSharp will not work with the Method.Option for the test post in the NetSuite Rest API Tutorial Postman sample. Wasted a day on that and then decided to try a Method.Post it it worked fine. Get the token information found in the tutorial at NetSuite Getting Started with Token-based Authentication

public dynamic GetEntity(int? entityid = null)
{
    string url = _config.ApiRoot + "/query/v1/suiteql";
    string sql = "SELECT * FROM entity ";
            
    if (entityid != null){
        sql += " WHERE id = " + entityid.ToString();                    
    }

    var authenticator = OAuth1Authenticator.ForAccessToken(
        _config.ClientId,
        _config.ClientSecret,
        _config.TokenId,
        _config.TokenSecret,
        OAuthSignatureMethod.HmacSha256);
    authenticator.Realm = _config.AccountId;

    var content = "{\"q\":\"" + sql + "\"}";

    var options = new RestClientOptions(url);
    options.Authenticator = authenticator;
    var client = new RestClient(options);
    var httpRequest = new RestRequest(url, Method.Post);
    httpRequest.AddHeader("prefer", "transient");
    httpRequest.AddBody(content);
    var httpResponse = client.Execute(httpRequest);

    if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
    {
        dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(httpResponse.Content);
        return obj;
    }
    else
    {
        return null;
    }