{"id":973,"date":"2023-06-10T19:51:48","date_gmt":"2023-06-10T19:51:48","guid":{"rendered":"http:\/\/fredborg.org\/?p=973"},"modified":"2023-06-10T19:51:48","modified_gmt":"2023-06-10T19:51:48","slug":"using-http-client-send-in-business-central","status":"publish","type":"post","link":"https:\/\/fredborg.org\/?p=973","title":{"rendered":"Using HTTP Client Send In Business Central"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This is going to be a short post, but I ran into some difficulties calling a web service the other day, and while using the HTTP client to call web services is something that I have done a thousand times, I have always used the HttpClient.Post() however this time I could not use the HttpClient.Post() but I had to use the HttpClient.Send() because I had to send the Accept Header, which you cannot set on the HttpContent but you have to set it on the HttpRequestMessage. Another thing that I had to do which I am not accustomed to was that I had to send an Authorization header as a Bearer token, now this does not have anything to do with using the Post() or the Send() but anyhow to add an Authorization header you must do the following to your HttpClient.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nClient.Clear();\nClient.DefaultRequestHeaders.Add(&#039;Authorization&#039;, StrSubstNo(&#039;Bearer %1&#039;, &#039;{{TOKEN}}&#039;));\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">For some reason, I had to clear my HttpClient before I could set my header, now the complete code that I used to call the Http endpoint using send is below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n    procedure PostJsonUsingSend(Url: Text; PayLoad: text): Text\n    var\n        Result: Text;\n        Content: HttpContent;\n        Response: HttpResponseMessage;\n        Request: HttpRequestMessage;\n        Client: HttpClient;\n        RequestHeader: HttpHeaders;\n        ContentHeader: HttpHeaders;\n    begin\n        \/\/Set auth\n        Client.Clear();\n        Client.DefaultRequestHeaders.Add(&#039;Authorization&#039;, StrSubstNo(&#039;Bearer %1&#039;, &#039;{TOKEN}}&#039;));\n        \/\/Request headers\n        Request.GetHeaders(RequestHeader);\n        RequestHeader.Add(&#039;Accept&#039;, &#039;application\/json&#039;);\n        \/\/Set content\n        Content.WriteFrom(PayLoad);\n        Request.Content := Content;\n        \/\/Content headers\n        Request.Content.GetHeaders(ContentHeader);\n        ContentHeader.Remove(&#039;Content-Type&#039;);\n        ContentHeader.Add(&#039;Content-Type&#039;, &#039;application\/json&#039;);\n        \/\/Set URL and type\n        Request.SetRequestUri(Url);\n        Request.Method(&#039;post&#039;);\n        \/\/Call HTTP\n        Client.Send(Request, Response);\n        Content := Response.Content;\n        response.Content().ReadAs(Result);\n        exit(Result);\n    end;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What you have to pay attention to when using Send is that you must set the Content on the HttpRequestMessage this is done with the following lines:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n \/\/Set content\nContent.WriteFrom(PayLoad);\nRequest.Content := Content;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you do not do this you will not send your payload and depending on the endpoint you might get back an error telling you that you are not allowed to call the endpoint with a blank payload.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And that is it for this short blog post, I hope it might help some of you not to waste the same amount of time that I did trying to get my Send() to work until next time stay safe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is going to be a short post, but I ran into some difficulties calling a web service the other&#8230; <a class=\"read-more\" href=\"https:\/\/fredborg.org\/?p=973\" rel=\"nofollow\"> Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-central","shapla-grid-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using HTTP Client Send In Business Central - Fredborg<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fredborg.org\/?p=973\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using HTTP Client Send In Business Central - Fredborg\" \/>\n<meta property=\"og:description\" content=\"This is going to be a short post, but I ran into some difficulties calling a web service the other... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fredborg.org\/?p=973\" \/>\n<meta property=\"og:site_name\" content=\"Fredborg\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-10T19:51:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central-1024x576.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"dfredborg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:site\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dfredborg\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fredborg.org\/?p=973#article\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/?p=973\"},\"author\":{\"name\":\"dfredborg\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"headline\":\"Using HTTP Client Send In Business Central\",\"datePublished\":\"2023-06-10T19:51:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=973\"},\"wordCount\":288,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=973#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png\",\"articleSection\":[\"Business Central\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fredborg.org\/?p=973#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fredborg.org\/?p=973\",\"url\":\"https:\/\/fredborg.org\/?p=973\",\"name\":\"Using HTTP Client Send In Business Central - Fredborg\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=973#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=973#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png\",\"datePublished\":\"2023-06-10T19:51:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fredborg.org\/?p=973#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fredborg.org\/?p=973\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/?p=973#primaryimage\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png\",\"width\":2560,\"height\":1440},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fredborg.org\/?p=973#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fredborg.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using HTTP Client Send In Business Central\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fredborg.org\/#website\",\"url\":\"https:\/\/fredborg.org\/\",\"name\":\"Fredborg\",\"description\":\"Business Central and Azure\",\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fredborg.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\",\"name\":\"dfredborg\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"width\":256,\"height\":256,\"caption\":\"dfredborg\"},\"logo\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:fredborg.org\",\"https:\/\/www.linkedin.com\/in\/dfredborg\/\",\"https:\/\/x.com\/dfredborg\",\"https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA\"],\"url\":\"https:\/\/fredborg.org\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using HTTP Client Send In Business Central - Fredborg","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fredborg.org\/?p=973","og_locale":"en_US","og_type":"article","og_title":"Using HTTP Client Send In Business Central - Fredborg","og_description":"This is going to be a short post, but I ran into some difficulties calling a web service the other... Read more","og_url":"https:\/\/fredborg.org\/?p=973","og_site_name":"Fredborg","article_published_time":"2023-06-10T19:51:48+00:00","og_image":[{"width":1024,"height":576,"url":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central-1024x576.png","type":"image\/png"}],"author":"dfredborg","twitter_card":"summary_large_image","twitter_creator":"@dfredborg","twitter_site":"@dfredborg","twitter_misc":{"Written by":"dfredborg","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fredborg.org\/?p=973#article","isPartOf":{"@id":"https:\/\/fredborg.org\/?p=973"},"author":{"name":"dfredborg","@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"headline":"Using HTTP Client Send In Business Central","datePublished":"2023-06-10T19:51:48+00:00","mainEntityOfPage":{"@id":"https:\/\/fredborg.org\/?p=973"},"wordCount":288,"commentCount":0,"publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"image":{"@id":"https:\/\/fredborg.org\/?p=973#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png","articleSection":["Business Central"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fredborg.org\/?p=973#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fredborg.org\/?p=973","url":"https:\/\/fredborg.org\/?p=973","name":"Using HTTP Client Send In Business Central - Fredborg","isPartOf":{"@id":"https:\/\/fredborg.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fredborg.org\/?p=973#primaryimage"},"image":{"@id":"https:\/\/fredborg.org\/?p=973#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png","datePublished":"2023-06-10T19:51:48+00:00","breadcrumb":{"@id":"https:\/\/fredborg.org\/?p=973#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fredborg.org\/?p=973"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/?p=973#primaryimage","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/Using-HTTP-Client-Send-In-Business-Central.png","width":2560,"height":1440},{"@type":"BreadcrumbList","@id":"https:\/\/fredborg.org\/?p=973#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fredborg.org\/"},{"@type":"ListItem","position":2,"name":"Using HTTP Client Send In Business Central"}]},{"@type":"WebSite","@id":"https:\/\/fredborg.org\/#website","url":"https:\/\/fredborg.org\/","name":"Fredborg","description":"Business Central and Azure","publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fredborg.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d","name":"dfredborg","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","width":256,"height":256,"caption":"dfredborg"},"logo":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/"},"sameAs":["https:fredborg.org","https:\/\/www.linkedin.com\/in\/dfredborg\/","https:\/\/x.com\/dfredborg","https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA"],"url":"https:\/\/fredborg.org\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=973"}],"version-history":[{"count":3,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/973\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/973\/revisions\/976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/media\/977"}],"wp:attachment":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}