curl 为什么我的PHP post函数在NodeJS Express中接收时是这样的格式?

t8e9dugd  于 4个月前  发布在  PHP
关注(0)|答案(1)|浏览(41)

伙计们,我对PHP很陌生,有一个函数试图将数据发布到我的后端NodeJS Express API端点。
收到的req.body在我收到的时候变成了这样。
它追加了额外的**{ jsonObj:“"}**

{{"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":{"name":"Steven Zhou","phone":"7788889128","email":"[email protected]"},"teamAdvisors":"TBD","addresses":{"street":"11451 4th ave","province":"BC","country":"Canada","zipcode":"V7E3H1"},"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}: ""}

字符串
我希望它是这样的格式

{"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":{"name":"Steven Zhou","phone":"7788889128","email":"[email protected]"},"teamAdvisors":"TBD","addresses":{"street":"11451 4th ave","province":"BC","country":"Canada","zipcode":"V7E3H1"},"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}


下面是发送POST请求的PHP代码

$data = [
        'name' => 'PHP Game test',
        'email' => '[email protected]',
        'logo' => 'logo.jpg',
        'heroImage' => 'hero.jpg',
        'teamName' => 'test',
        'teamSize' => '4',
        "type" => "publisher",
        "mainContacts"=> [
            "name"=> "Steven Zhou",
            "phone"=> "7788889128",
            "email"=> "[email protected]"
        ],
        "teamAdvisors"=> "TBD",
        "addresses"=> [
            "street"=> "11451 4th ave",
            "province"=> "BC",
            "country"=> "Canada",
            "zipcode"=> "V7E3H1"
        ],
        "url"=> "appcodes.io",
        "officeLocations"=> "5",
        "gamesProduced"=> "2",
        "gamesInDevelopment"=> "3",
        "description"=> "Test API"
    ];

    $jsonData = json_encode($data, JSON_FORCE_OBJECT);
    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://localhost:3006/api/company",
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $jsonData,
        CURLOPT_HTTPHEADER => array(
            "contentType : application/json",
            "Accept: */*",
            "Connection: keep-alive",
            "Access-Control-Allow-Origin:*",
            "Accept-Encoding: gzip, deflate, br",
            "api-key: ..."
        ),
    ));
    $response = curl_exec($curl);
    echo $response;
    $err = curl_error($curl);
    curl_close($curl);


当我不使用json_encode函数时,它会正确发送数据,除非地址和联系人变成了没有键的普通数组,如下图所示。

{"name":"PHP Game test","email":"[email protected]","logo":"logo.jpg","heroImage":"hero.jpg","teamName":"test","teamSize":"4","type":"publisher","mainContacts":["Steven Zhou","7788889128","[email protected]"],"teamAdvisors":"TBD","addresses":["11451 4th ave","BC","Canada","V7E3H1"],"url":"appcodes.io","officeLocations":"5","gamesProduced":"2","gamesInDevelopment":"3","description":"Test API"}


下面是接收请求的NodeJS Express端点

router.post('/company', multer.none(), verifyWordpressApiKey, createCompany);

async function createCompany ( req, res, next){
    try{
        if(req.body.logo.length === 0) {
            return res.status(400).send({errorMessage: 'No company logo uploaded.'});
        } else if(req.body.heroImage.length === 0) {
            return res.status(400).send({errorMessage: 'No hero image uploaded.'});
        }

        req.body.createdBy = DB_WORDPRESS_USER;
        let jsonObj = JSON.parse(req.body.mainContacts);
        const organizationResult = await createOrganizationFromPipedrive(req.body.name);
        await createNewPersonFromPipedrive(organizationResult.id,
            jsonObj[0],
            jsonObj[1],
            jsonObj[2]);
        await createInternalCompany(req.body);
        res.sendStatus(200);
    }catch (error) {
        next(new Error('Unable to create a new company' + error));
    }
}


我很困惑,任何帮助都很感激。
谢谢你,谢谢

6xfqseft

6xfqseft1#

CURL请求中将contentType更改为Content-Type
并确保在Express中使用

app.use(express.json());// this will format all incoming request as JSON

字符串

相关问题