我试图通过Acumatica的REST创建销售订单和发票。由于某些原因,我只在通过创建发票时出错,我对这两种情况都使用了几乎相同的JSON:
这是我的RestService:
public class RestService : IDisposable
{
private readonly HttpClient _httpClient;
private readonly string _acumaticaBaseUrl;
#region Ctor
public RestService(string acumaticaBaseUrl, string userName, string password, string company, string branch, string locale)
{
_acumaticaBaseUrl = acumaticaBaseUrl;
_httpClient = new HttpClient(
new HttpClientHandler
{
UseCookies = true,
CookieContainer = new CookieContainer()
})
{
BaseAddress = new Uri(acumaticaBaseUrl + "/entity/Default/6.00.001/"),
DefaultRequestHeaders =
{
Accept = {MediaTypeWithQualityHeaderValue.Parse("text/json") }
}
};
//Log in to Acumatica ERP
_httpClient.PostAsJsonAsync(
acumaticaBaseUrl + "/entity/auth/login", new
{
name = userName,
password = password,
company = company,
branch = branch,
locale = locale
}).Result
.EnsureSuccessStatusCode();
}
void IDisposable.Dispose()
{
_httpClient.PostAsync(_acumaticaBaseUrl + "/entity/auth/logout",
new ByteArrayContent(new byte[0])).Wait();
_httpClient.Dispose();
}
#endregion
//Data submission
public string Put(string entityName, string parameters, string entity)
{
var res = _httpClient.PutAsync(_acumaticaBaseUrl + "/entity/Default/6.00.001/" + entityName + "?" + parameters, new StringContent(entity, Encoding.UTF8, "application/json")).Result
.EnsureSuccessStatusCode();
return res.Content.ReadAsStringAsync().Result;
}
}以下是主要代码:
using (RestService service = new RestService(ACUMATICA_INSTANCE_URL, USERNAME,PASSWORD,COMPANY, "", "EN-US"))
{
Console.WriteLine("Login successful");
string order = @"{
""OrderNbr"":{ value: ""000204"" },
""CustomerID"":{ value: ""TESTCST005""},
""Details"": [
{
""InventoryID"": { value: ""301CMPST02"" },
""Quantity"": { value: 10 }
}]
}";
string invoice = @"{
""ReferenceNbr"":{ value: ""001032"" },
""CustomerID"":{ value: ""TESTCST005""},
""Details"": [
{
""InventoryID"": { value: ""DESIGN"" },
""Quantity"": { value: 10 }
}]
}";
try
{
Console.WriteLine("Trying to create the following Order");
Console.WriteLine(order);
string updatedOrder = service.Put("SalesOrder", null, order);
Console.WriteLine("Order created successful");
Console.WriteLine(updatedOrder);
Console.WriteLine("Trying to create the following Invoice");
Console.WriteLine(invoice);
string updatedInvoice = service.Put("SalesInvoice", null, invoice);
Console.WriteLine("Invoice created successful");
Console.WriteLine(updatedInvoice);
}
catch(Exception exc)
{
Console.WriteLine(exc.Message);
}
Console.ReadLine();
}我找不出是什么问题。我得到的回应总是如下:
“Acumatica-PX.Export/AuthenticationManagerModule\r\n : 500,ReasonPhrase:‘内部服务器错误’,版本: 1.1,Content: System.Net.Http.StreamContent,Header:\private Control:私有\en集-ReasonPhrase:Locale=TimeZone=GMTM0500G&System.Net.Http.StreamContent=en;path=/\r\n Set-Cookie: UserBranch=5;path=/\r\n服务器:Microsoft/8.5\r\n X驱动-By: ASP.NET\r\n日期: Wed,2017年8月2日13: 02 :49 GMT\r\n内容-长度: 36\r\n内容-类型: text/json;charset=utf-8\r\n}“
我可以用相同的值从屏幕上添加发票。下面是使用Acumatica创建客户记录的创建的示例。
发布于 2017-08-01 19:42:57
我没有您的"PostAsJsonAsync“函数,所以必须恢复到Acumatica提供的文档中使用的方法,并使用以下方法:
string credentialsAsString = JsonConvert.SerializeObject(new
{
name = userName,
password = password,
//company = Properties.Settings.Default.CompanyName,
//branch = Properties.Settings.Default.Branch
});
var response = _httpClient.PostAsync(acumaticaBaseUrl + "/entity/auth/login", new StringContent(credentialsAsString, Encoding.UTF8, "application/json")).Result;但是,一旦我这样做,我发现的唯一错误就是一个JSON结构错误。您忘记将value关键字放在双引号之间。
这是:
string order = @"{
""OrderNbr"":{ value: ""000204"" },
""CustomerID"":{ value: ""TESTCST005""},
""Details"": [
{
""InventoryID"": { value: ""301CMPST02"" },
""Quantity"": { value: 10 }
}]
}";应通过以下几点加以纠正:
string order = @"{
""OrderNbr"":{ ""value"": ""000204"" },
""CustomerID"":{ ""value"": ""TESTCST005""},
""Details"": [
{
""InventoryID"": { ""value"": ""301CMPST02"" },
""Quantity"": { ""value"": 10 }
}]
}";发布于 2018-11-28 06:20:42
我也解决了同样的问题。
问题是客户"TESTCST005“并不存在于系统中。您需要创建一个新客户:)
https://stackoverflow.com/questions/45438701
复制相似问题