Bo Damgaard Mortensen
04/26/2023, 12:39 PMTimothy Jones
04/26/2023, 1:11 PMTimothy Jones
04/26/2023, 1:11 PMBo Damgaard Mortensen
04/26/2023, 2:38 PMTimothy Jones
04/26/2023, 3:13 PMTimothy Jones
04/26/2023, 3:13 PMTimothy Jones
04/26/2023, 3:14 PMTimothy Jones
04/26/2023, 3:16 PMTimothy Jones
04/26/2023, 3:16 PMYousaf Nabi (pactflow.io)
Bo Damgaard Mortensen
04/27/2023, 6:08 AMHttpClient
in .net to publish the contracts, just to get started. However, I get a 400 bad request when I try pondering
My code looks like this:
byte[] jsonBytes = File.ReadAllBytes("../../../../../pacts/mypactfile.json");
var base64 = Convert.ToBase64String(jsonBytes);
dynamic payload = new
{
pacticipantName = "my participant name",
pacticipantVersionNumber = 1,
contracts = new dynamic[]
{
new
{
consumerName = "my consumer",
providerName = "my provider",
specification = "pact",
contentType = "application/json",
content = base64
}
}
};
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("<https://myaccount.pactflow.io/>");
HttpRequestMessage message = new(<http://HttpMethod.Post|HttpMethod.Post>, "contracts/publish");
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "my api token from <http://pactflow.io|pactflow.io>");
message.Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = httpClient.Send(message);
if (!responseMessage.IsSuccessStatusCode)
{
throw new Exception($"Failed to publish pacts. Status code: {responseMessage.StatusCode}");
}
Any idea of what I did wrong here? :-)Timothy Jones
04/27/2023, 6:32 AMnew AuthenticationHeaderValue(“Bearer”, “my api token from pactflow.io”);I don’t know what this does specifically, but in case you hand rolled it, the header must be
Bearer YOURTOKEN
(note the space)
Otherwise yours looks ok. In mine, I also have tags: []
, and Accept: 'application/json'
.Timothy Jones
04/27/2023, 6:33 AMAccept: 'application/hal+json',
Timothy Jones
04/27/2023, 6:35 AMTimothy Jones
04/27/2023, 6:35 AMBo Damgaard Mortensen
04/27/2023, 6:36 AMTimothy Jones
04/27/2023, 6:38 AMTimothy Jones
04/27/2023, 6:39 AMTimothy Jones
04/27/2023, 6:40 AMapplication/hal+json
Bo Damgaard Mortensen
04/27/2023, 6:40 AMapplication/json
, with out the hal+json
🙂Timothy Jones
04/27/2023, 6:41 AMBo Damgaard Mortensen
04/27/2023, 6:41 AMHttpResponseMessage responseMessage = httpClient.Send(message);
if (!responseMessage.IsSuccessStatusCode)
{
string error = await responseMessage.Content.ReadAsStringAsync();
throw new Exception($"{error}. Status code: {responseMessage.StatusCode}");
}
Timothy Jones
04/27/2023, 6:41 AMTimothy Jones
04/27/2023, 6:41 AMBo Damgaard Mortensen
04/27/2023, 6:41 AMBo Damgaard Mortensen
04/27/2023, 6:47 AMTimothy Jones
04/27/2023, 7:55 AM