RestSharp - StatusCode:UnsupportedMediaType

时间:2016-07-11 17:47:09

标签: c# restsharp

我正在尝试将新客户发布到网络API。当我在Fiddler中执行以下操作时,我没有错误:

POST https://api.someurl.com/api/businesses/99999999/contacts HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Accept: application/json
Host: api.someurl.com
Authorization: TOKEN json:{"authenticationCode":"kLJBlaUCK0pYY6YXI6qB3CHamq0=","expiryDate":1468083160641,"locale":"en_US","myUserId":88888888888,"restriction":null,"site":"api.someurl.com"}
Content-Length: 931

{"business":9999999999,"collaborationContext":0,"contactInformation":{"campaign":null,"city":"SomeCity","country":"SomeCountry","email":"email@someurl.com","fax":null,"first":"FName","hasShippingAddress":false,"instructions":null,"last":"LName","locale":null,"mobile":null,"name":"FullName","phone":"222.333.4455","postalOrZipCode":"90210","provinceOrState":null,"readOnly":false,"referral":null,"registration":null,"shippingCity":null,"shippingCountry":null,"shippingName":null,"shippingNotes":null,"shippingPhone":null,"shippingPostalOrZipCode":null,"shippingProvinceOrState":null,"shippingStreet1":null,"shippingStreet2":null,"street1":null,"street2":null,"tollFree":null,"url":null},"currency":null,"defaultTaxCode":null,"id":0,"incomeOrExpenseAccount":null,"linkedBusiness":null,"payableAccount":null,"paymentAccount":null,"paymentTerms":null,"prefix":null,"receivableAccount":null,"removed":false,"type":"CUSTOMER"}

当我尝试执行(我认为)等价物(见下文)时,我得到“StatusCode:UnsupportedMediaType,Content-Type:text / xml,Content-Length:0)”

private static string PostContact(string busId, Contact contact )
        {
            var restClient = new RestClient("https://api.someurl.com");
            var jsonToken = GetJsonToken();
            // var contactJson = JsonConvert.SerializeObject(contact);
            var req = new RestRequest("/api/businesses/{business-id}/contacts");
            req.Method = Method.POST;
            req.Parameters.Clear();
            req.AddHeader("Content-Type", "application/json");
            req.AddHeader("Accept", "application/json");
            req.AddUrlSegment("business-id", busId);
            req.AddParameter("Authorization", string.Format("TOKEN json:{0}", jsonToken), ParameterType.HttpHeader);
            req.AddParameter("application/json", contact, ParameterType.RequestBody);
            var resp = restClient.Execute(req);
            Console.WriteLine(resp.StatusCode);
            return resp.Content;
        }

添加参数或标题是否有特殊顺序,或者我遗漏了什么?

1 个答案:

答案 0 :(得分:0)

已解决!

var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
///request.AddHeader("Authorization", "Bearer " + access_token);
request.AddHeader("Authorization", token);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", JsonRequest, ParameterType.RequestBody);
var response = client.Execute(request);
var responseJson = response.Content;