簡體   English   中英

如何使用 Excel Z6E3EC7E6A9F6007B4838FC0EE79 獲取 curl Api 請求代碼?

[英]How to get curl Api request code using Excel VBA?

我有一個 API“GET”請求代碼,用於從我的 crm 獲取 session 密鑰到 Excel。

我正在嘗試重新調整它的用途,以便在“PUT”請求中將信息發送到我的 crm。

當我執行Open "Put", webServiceURL, False時出現“錯誤請求”錯誤。

當我這樣做時,我得到了所有數據。 .Open "Get", webServiceURL, False (只是沒有改變任何東西)。

Sub UpdateEstimate()

    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

我作為 PutEstJson 發送的 json 格式數據的副本。

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' 

如果不知道您嘗試調用的 API 方法的詳細信息,恐怕這將很難幫助進行故障排除。 該錯誤表明您嘗試發布的 JSON 有問題。 一些對我來說突出的潛在問題是:

  1. 您的 JSON 中似乎缺少一堆[ , ] , { , }字符。 該結構格式錯誤,因此 JSON 無效,這肯定會導致您的錯誤。

  2. 您的 JSON 密鑰可能應該在雙引號內以構成有效的 JSON。

     `{ "Id": 0, "Name": "string" }...`
  3. 我注意到 JSON 的末尾有一個流氓逗號...這絕對不是有效的 JSON。

我建議將您的 JSON 放在可以格式化 JSON (如vscode )的文本編輯器上。 這應該可以幫助您更好地了解 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM