skip to Main Content

I tried a few different ways I can’t get pipelineId to post (receive 400), see code below:

{
                client.BaseAddress = new Uri(_serviceConfig.DataGapsBaseUrl);
                var request = new HttpRequestMessage(HttpMethod.Post, "/piper/jobs");

                var jsonContent = new StringContent(JsonConvert.SerializeObject(new
                {
                    pipelineId = _serviceConfig.DataPipelineId
                }), Encoding.UTF8, "application/json");
                
                request.Content = jsonContent;

                var token = await GetToken();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.ToString());

                var result = await client.SendAsync(request);

                var json = JsonConvert.DeserializeObject<JToken>(result.Content.ReadAsStringAsync().Result);
                var jobId = json["id"].ToString();
                return jobId;
            }
        }

When I use Postman same actions as above, I get 200 status and results:

enter image description here

3

Answers


  1. Chosen as BEST ANSWER

    Sorry Guys, my fault I need to send this:

    {"pipelineId":"zxxzxzxzxz18","inputs":{"Batch_Id":9999}}

    Needed to figure out how to hand double quotes and it fixed the issue.


  2. since you are using async await , use it everywhere and this syntax is more usuall

    using HttpClient client = new HttpClient { BaseAddress = new Uri(baseUri) };
    
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.ToString());
    
    var jsonContent = new StringContent(JsonConvert.SerializeObject(new
                    {
                        pipelineId = _serviceConfig.DataPipelineId
                    }), Encoding.UTF8, "application/json");
    
     var response = await client.PostAsync(uri, jsonContent);
    
     var json = await response.Content.ReadAsStringAsync();
    
    var jObj = JObject.Parse(json);
    var createTime =  (string) jObj["createTime"]; //I can't see Id in Postman
    
    Login or Signup to reply.
  3. For starters:

    • You forgot to serialize the StringContent
    var jsonContent = new StringContent(JsonConvert.SerializeObject(new
                    {
                        pipelineId = _serviceConfig.DataPipelineId
                    }), Encoding.UTF8, "application/json");
    

    Change the following using await aswell to prevent thread-locking

    var json = JsonConvert.DeserializeObject<JToken>(await result.Content.ReadAsStringAsync());
    

    Please note if your client is a singleton and you are setting authentication from an async context it might be overwritten by another call either recreate client or perhaps set the authorization on the HttpRequestMessage:

    var request = new HttpRequestMessage(HttpMethod.Post, "/piper/jobs");
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.ToString()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search