需要帮助使用excel vba使curl Api请求代码正常工作。

问题描述 投票:0回答:1

我有一个API "GET "请求代码,我用来从我的crm获取会话密钥到excel中.我试图重新使用它来发送信息到我的crm的 "PUT "请求,但有些东西是不正确的翻译,因为我一直得到一个 "坏的请求 "错误,当我做"。 Open "Put", webServiceURL, False" in the code but I get all the data when I do " .Open "Get", webServiceURL, False"(just does not change anything.) if anyone is willing to help this amateur code enthusiast I would be very great-full to you.下面我包括了我到目前为止所做的代码,它给我一个 "bad Request "错误。


  Dim webServiceURL As String
  Dim actionType1 As String
  Dim targetWord1 As String
  Dim actionType2 As String
  Dim targetWord2 As String
  Dim PutEstJson  As String
  Dim APISettings As Worksheet
   Dim res As Variant
  Dim allres As Variant
  Dim Token As String
Dim scriptControl As Object
  Token = Worksheets("API Settings").Range("e3").Value
  'get token data
  website = "https://cloud.servicebridge.com/api/v1.1/Estimates/25014108?sessionKey="
  webServiceURL = website & Token

  actionType2 = "Accept"
  targetWord2 = "application/json"
  actionType1 = "Content-Type"
  targetWord1 = "application/json"

  PutEstJson = Worksheets("API Settings").Range("k7").Value

  With CreateObject("WinHttp.WinHttpRequest.5.1")

    .Open "Put", webServiceURL, False
    .setRequestHeader actionType2, targetWord2
    .setRequestHeader actionType1, targetWord1

    .Send PutEstJson
     allres = .GetAllResponseHeaders
     res = .responseText
     APIKey = Split(res, "Data")


    If .Status = 200 Then

    'AVAILABLE INFORMATION
    '_____________________
       ' Debug.Print .Status
        'Debug.Print .responseText
       ' Debug.Print .GetAllResponseHeaders
        'MsgBox .GetAllResponseHeaders


'paste token data






        Worksheets("API Settings").Cells(4, 3).Value = Split(res, "data")
        Worksheets("API Settings").Cells(4, 4).Value = allres



    Else
      MsgBox .Status & ": " & .StatusText
    End If

    End With

End Sub```

heres a copy of the json format data i send as PutEstJson.

```curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{  
Location:   {
Id: 0,
Name:   "string"
},  
UpdateLocation: false,
GeoCoordinates: {
Latitude:   0,
Longitude:  0
},  
UpdateGeoCoordinates:   false,
Contact:    {
Id: 0,
Name:   "string"
},  
UpdateContact:  false,
ThirdPartyBillPayer:    {
Id: 0,
Name:   "string"
},  
UpdateThirdPartyBillPayer:  false,
MarketingCampaign:  {
Id: 101002,
Name:   "Yard Sign"
},  
UpdateMarketingCampaign:    false,
JobCategory:    {
Id: 21412,
Name:   "Finished Estimate: Hot Lead"
},  
UpdateJobCategory:  true,
SalesRepresentative:    {
Id: 382891,
Name:   "Marvin Lamar"
},  
UpdateSalesRepresentative:  true,
DefaultEquipment:   {
Id: 0,
Name:   "string"
},  
UpdateDefaultEquipment: false,
Description:    "Gutter/Guard Estimate",
UpdateDescription:  true,
Status: "Assigned",
UpdateStatus:   false,
Branch: {
Id: 0,
Name:   "string"
},  
UpdateBranch:   false,
Team:   {
Id: 188466,
Name:   "Yerry"
},  
UpdateTeam: false,
ConfirmationStatus: "None",
UpdateConfirmationStatus:   false,
EstimateDate:   "2020-04-15T04:46:02.867Z",
UpdateEstimateDate: false,
ScheduledTime:  0,
UpdateScheduledTime:    false,
EstimatedDuration:  0,
UpdateEstimatedDuration:    false,
ArrivalWindow:  0,
UpdateArrivalWindow:    false,
EarliestArrival:    0,
UpdateEarliestArrival:  false,
LatestDeparture:    0,
UpdateLatestDeparture:  false,
Notes:  "string",
UpdateNotes:    false,
PrivateNotes:   "string",
UpdatePrivateNotes: false,
InvoiceNotes:   "string",
UpdateInvoiceNotes: false,
UpdateReminder: false,
ReminderType:   "None",
ReminderValue:  0,
ReminderMessage:    "string",
TaxCalculation: "TaxExcluded",
UpdateTaxCalculation:   false,
CustomFields:   [
{   
Name:   "S Walk Around Done",
Value:  "",
Name:   "S4 Est Rating",
Value:  "",
Name:   "S2 I went for",
Value:  "",
Name:   "S3 I Feel Est. Will Close In",
Value:  "",
Name:   "S7 1st Follow Up Date",
Value:  "",
Name:   "S7 2nd Follow Up Date",
Value:  "",
Name:   "S7 3rd Follow Up Date",
Value:  "",
Name:   "Follow-up Notes",
Value:  "",
}   
],  
UpdateCustomFields: false,
SparseUpdate: "True" ,
Version:    10,
ExternalSystemId:   "string",
UpdateExternalSystemId: false,
}   
' 'https://cloud.servicebridge.com/api/v1.1/Estimates/25014108?sessionKey=6550e422e843f1d94e2e8c441e05d7197f0b871' ```
excel vba rest curl put
1个回答
1
投票

如果不知道你试图调用的API方法的细节,恐怕很难帮助解决这个问题。这个错误表明你试图发布的JSON有问题。在我看来,有几个潜在的问题是这样的。

  1. 似乎有一堆 [,],{,} 您的JSON中缺少字符。该结构是畸形的,因此是无效的JSON,这肯定会导致你的错误。

  2. 你的JSON键可能应该放在双引号内,以构成有效的JSON。

    `{
       "Id": 0,
       "Name": "string"
    } ...`
    
  3. 我注意到你的JSON的末尾有一个逗号...这绝对不是有效的JSON。

我建议把你的JSON放在一个可以格式化JSON的文本编辑器上(如 编码). 这应该能帮助你更好地看到JSON的结构。我不太清楚JSON的结构应该是什么,所以这是我对它的最佳猜测。我不得不猜测缺失的 [,],{,} 角色应该是。

{
    "Id": 0,
    "Name": "string",
    "UpdateLocation": false,
    "GeoCoordinates": {
        "Latitude": 0,
        "Longitude": 0
    },
    "UpdateGeoCoordinates": false,
    "Contact": {
        "Id": 0,
        "Name": "string"
    },
    "UpdateContact": false,
    "ThirdPartyBillPayer": {
        "Id": 0,
        "Name": "string"
    },
    "UpdateThirdPartyBillPayer": false,
    "MarketingCampaign": {
        "Id": 101002,
        "Name": "Yard Sign"
    },
    "UpdateMarketingCampaign": false,
    "JobCategory": {
        "Id": 21412,
        "Name": "Finished Estimate: Hot Lead"
    },
    "UpdateJobCategory": true,
    "SalesRepresentative": {
        "Id": 382891,
        "Name": "Marvin Lamar"
    },
    "UpdateSalesRepresentative": true,
    "DefaultEquipment": {
        "Id": 0,
        "Name": "string"
    },
    "UpdateDefaultEquipment": false,
    "Description": "Gutter/Guard Estimate",
    "UpdateDescription": true,
    "Status": "Assigned",
    "UpdateStatus": false,
    "Branch": {
        "Id": 0,
        "Name": "string"
    },
    "UpdateBranch": false,
    "Team": {
        "Id": 188466,
        "Name": "Yerry"
    },
    "UpdateTeam": false,
    "ConfirmationStatus": "None",
    "UpdateConfirmationStatus": false,
    "EstimateDate": "2020-04-15T04:46:02.867Z",
    "UpdateEstimateDate": false,
    "ScheduledTime": 0,
    "UpdateScheduledTime": false,
    "EstimatedDuration": 0,
    "UpdateEstimatedDuration": false,
    "ArrivalWindow": 0,
    "UpdateArrivalWindow": false,
    "EarliestArrival": 0,
    "UpdateEarliestArrival": false,
    "LatestDeparture": 0,
    "UpdateLatestDeparture": false,
    "Notes": "string",
    "UpdateNotes": false,
    "PrivateNotes": "string",
    "UpdatePrivateNotes": false,
    "InvoiceNotes": "string",
    "UpdateInvoiceNotes": false,
    "UpdateReminder": false,
    "ReminderType": "None",
    "ReminderValue": 0,
    "ReminderMessage": "string",
    "TaxCalculation": "TaxExcluded",
    "UpdateTaxCalculation": false,
    "CustomFields": [
        {
            "Name": "S Walk Around Done",
            "Value": ""
        },
        {
            "Name": "S4 Est Rating",
            "Value": ""
        },
        {
            "Name": "S2 I went for",
            "Value": ""
        },
        {
            "Name": "S3 I Feel Est. Will Close In",
            "Value": ""
        },
        {
            "Name": "S7 1st Follow Up Date",
            "Value": ""
        },
        {
            "Name": "S7 2nd Follow Up Date",
            "Value": ""
        },
        {
            "Name": "S7 3rd Follow Up Date",
            "Value": ""
        },
        {
            "Name": "Follow-up Notes",
            "Value": "test1"
        }
    ],
    "UpdateCustomFields": true,
    "SparseUpdate": "True",
    "Version": 11,
    "ExternalSystemId": "string",
    "UpdateExternalSystemId": false
}
© www.soinside.com 2019 - 2024. All rights reserved.