gpt4 book ai didi

c# - 可以使用 API GET 但不能使用 API POST

转载 作者:IT老高 更新时间:2023-10-28 23:08:10 24 4
gpt4 key购买 nike

我正在 VS 2013 中处理现有的 Windows 服务项目。

我添加了一个 Web API Controller 类,我现在不记得它是 (v2.1) 还是 (v1) Controller 类...。无论如何,我已将其称为 SyncPersonnelViaAwsApiController

我试图从 AWS lambda 调用它...所以如果我调用 GET

public string Get(int id)
{
return "value";
}

with const req = https.request(' https://actualUrlAddress/api/SyncPersonnelViaAwsApi/Get/4 ', (res) => {

我得到 returned body: undefined"value" 这是正确的。 但是,如果我尝试打电话

const req = https.request('https://actualUrlAddress/api/SyncPersonnelViaAwsApi/SapCall', (res) => {

我得到returned body: undefined{"Message":"请求的资源不支持http方法'GET'。"}

 //// POST api/<controller>
public string SapCall([FromBody]string xmlFile)
{
string responseMsg = "Failed Import User";

if (!IsNewestVersionOfXMLFile(xmlFile))
{
responseMsg = "Not latest version of file, update not performed";
}
else
{
Business.PersonnelReplicate personnelReplicate = BusinessLogic.SynchronisePersonnel.BuildFromDataContractXml<Business.PersonnelReplicate>(xmlFile);
bool result = Service.Personnel.SynchroniseCache(personnelReplicate);

if (result)
{
responseMsg = "Success Import Sap Cache User";
}
}

return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";
}

有谁知道为什么它适用于 GET 而不是 POST?

因为我们可以点击让我确信它不是 lambda,但我已经将它包括在内以防万一

const AWS = require('aws-sdk');
const https = require('https');
var s3 = new AWS.S3();
var un;
var pw;
var seralizedXmlFile;


let index = function index(event, context, callback) {

// For the purpose of testing I have populated the bucket and key params with objects that already exist in the S3 bucket
var params = {
Bucket: "testbucketthur7thdec",
Key: "personnelData_50312474_636403151354943757.xml"
};


// Get Object from S3 bucket and add to 'seralizedXmlFile'
s3.getObject(params, function (data, err) {
console.log("get object from S3 bucket");
if (err) {
// an error occurred
}
else
{
console.log("data " + data);
// populate seralizedXmlFile with data from S3 bucket
let seralizedXmlFile = err.Body.toString('utf-8'); // Use the encoding necessary
console.log("objectData " + seralizedXmlFile);
}

});

// set params
var ssm = new AWS.SSM({ region: 'Usa2' });
console.log('Instatiated SSM');
var paramsx = {
'Names': ['/Sap/ServiceUsername', '/Sap/ServicePassword'],
'WithDecryption': true
};

// password and username
ssm.getParameters(paramsx, function (err, data) {
console.log('Getting parameter');
if (err) console.log(err, err.stack); // an error occurred
else {
console.log('data: ' + JSON.stringify(data)); // successful response
console.log('password: ' + data.Parameters[0].Value);
console.log('username: ' + data.Parameters[1].Value);
pw = data.Parameters[0].Value;
un = data.Parameters[1].Value;
}


// request to external api application & remove dependency on ssl
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

//POST DOES NOT WORK
const req = https.request('https://actualUrlAddress/api/SyncPersonnelViaAwsApi/SapEaiCall', (res) => {
//GET WORKS
// const req = https.request('https://actualUrlAddress/api/SyncPersonnelViaAwsApi/Get/4', (res) => {

res.headers + 'Authorization: Basic ' + un + ':' + pw;
let body = seralizedXmlFile;
console.log('seralizedXmlFile: ' + seralizedXmlFile);
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));

res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
callback(null, body);
console.log('returned body:', body);

});
});
req.end();
});
};
exports.handler = index;

更新感谢@Thangadurai 发布AWS Lambda - NodeJS POST request and asynch write/read file

我能够包含一个 post_options...请参阅更新的 lambda

          // An object of options to indicate where to post to
var post_options = {
host: 'https://actualUrlAddress',
port: '80',
path: '/api/SyncPersonnelViaAwsApi/SapEaiCall',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};

const req = https.request(post_options, (res) => {
res.headers + 'Authorization: Basic ' + un + ':' + pw;
let body = seralizedXmlFile;
console.log('seralizedXmlFile: ' + seralizedXmlFile);
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));

res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
callback(null, body);
console.log('returned body:', body);

});
});
req.end();

现在标记为错误:

Error: getaddrinfo ENOTFOUND http://actualUrlAddress http://actualUrlAddress.private:80

我之前遇到过这个 getaggrinfo ENOTFOUND 错误,这意味着它找不到地址....但是主机名和 api 路径是否正确?

我正在尝试联系

const req = https.request('https://actualUrlAddress/api/SyncPersonnelViaAwsApi/SapCall

是的,端口是 80

任何帮助将不胜感激塔M

最佳答案

直接跳到更新部分(据我了解,其他一切都不相关)。选项应如下所示:

var post_options = {
host: 'actualUrlAddress',
protocol: 'https:'
port: '443',
path: '/api/SyncPersonnelViaAwsApi/SapEaiCall',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};

自从 documentation states、host和protocol在两个独立的属性中,SSL端口不太可能是80,通常是443。

关于c# - 可以使用 API GET 但不能使用 API POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com