通过Restful API创建Azure Lambda Function - 不太稳定,时而遇到400Bad Request

简介: 通过Restful API创建Azure Lambda Function - 不太稳定,时而遇到400Bad Request

Our team is trying to create functions via Restful API as documented here:


https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createfunction


image.png

This API can work, sometimes.


We are testing it using postman with url:


https://management.azure.com/subscriptions//resourceGroups/ep-lambda-functions/providers/Microsoft.Web/sites/beforeCreateUser/functions/strange?api-version=2019-08-01


In HTTP request body, maintain the following JSON source code:

{

"properties": {

"files": {

"index.js": "console.log(\"Hello\");"

},

"test_data": "{\r\n    \"name\": \"Azure\"\r\n}",

"config": {

"bindings": [

{

 "authLevel": "function",

 "type": "httpTrigger",

 "direction": "in",

 "name": "req",

 "methods": ["get", "post"]

},

{

 "type": "http",

 "direction": "out",

 "name": "res"

}

],

"disabled": false

}

}

}

Execute the request in postman, sometimes the API returns 201 Created as expected. Unfortunately sometimes, we get HTTP 400 bad request instead:

image.png

Error detail:

{

   "Code": "BadRequest",

   "Message": "Encountered an error (InternalServerError) from host runtime.",

   "Target": null,

   "Details": [

       {

           "Message": "Encountered an error (InternalServerError) from host runtime."

       },

       {

           "Code": "BadRequest"

       },

       {

           "ErrorEntity": {

               "Code": "BadRequest",

               "Message": "Encountered an error (InternalServerError) from host runtime."

           }

       }

   ],

   "Innererror": null

}

We can ensure the correctness of our HTTP request payload.


When I make some deep research, I found a fact which makes me even more surprised.


I setup a jMeter project to fire 10 requests to Azure simultaneously:

image.png

For the name of functions to be created, I use prefix “jerryfuncv3_” plus {uuid}, which is a randiom integrate between 1 and 100:

image.png

only 2 among these 10 requests are successful:

image.png

Let’s check detail of one failed request, take this one for example, function name: jerryfuncv3_70

image.png

To my surprise(!!!), although the 8 requests return 400 bad request, still I could see all 10 functions are created in Azure, with status “Enabled” marked.

image.png

And for some times, the function creation would fail with 400 Bad request as well, even when I create it manually in Azure portal, as below:


image.png

{"properties":{"files":{"index.js":"module.exports = async function (context, req) {\r\n    context.log('JavaScript HTTP trigger function processed a request.');\r\n\r\n    if (req.query.name || (req.body && req.body.name)) {\r\n        context.res = {\r\n            // status: 200, /* Defaults to 200 */\r\n            body: \"Hello \" + (req.query.name || req.body.name)\r\n        };\r\n    }\r\n    else {\r\n        context.res = {\r\n            status: 400,\r\n            body: \"Please pass a name on the query string or in the request body\"\r\n        };\r\n    }\r\n};"},"test_data":"{\r\n    \"name\": \"Azure\"\r\n}","config":{"bindings":[{"authLevel":"anonymous","type":"httpTrigger","direction":"in","name":"req","methods":["get","post"]},{"type":"http","direction":"out","name":"res"}],"disabled":false}}}

400 Bad request would appear sometimes in Azure API console as well.

image.png

image.png

{

 "Code": "BadRequest",

 "Message": "Encountered an error (InternalServerError) from host runtime.",

 "Target": null,

 "Details": [

   {

     "Message": "Encountered an error (InternalServerError) from host runtime."

   },

   {

     "Code": "BadRequest"

   },

   {

     "ErrorEntity": {

       "Code": "BadRequest",

       "Message": "Encountered an error (InternalServerError) from host runtime."

     }

   }

 ],

 "Innererror": null

}

I searched related github issue and cannot find solution.


This issue below might be related, unfortunately no solution are provided.

https://github.com/microsoft/vscode-azurefunctions/issues/1838

image.png

image.png

In a nutshell, we need the API mentioned in the document below to be STABLE, which means, if the function creation is successfully, we expected 201 created status code. The current behavior is quite confusing indeed!


https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createfunction


相关文章
|
7天前
|
分布式计算 Java API
Java 8新特性之Lambda表达式与Stream API
【4月更文挑战第16天】本文将介绍Java 8中的两个重要新特性:Lambda表达式和Stream API。Lambda表达式是Java 8中引入的一种新的编程语法,它允许我们将函数作为参数传递给其他方法,从而使代码更加简洁、易读。Stream API是Java 8中引入的一种新的数据处理方式,它允许我们以声明式的方式处理数据,从而使代码更加简洁、高效。本文将通过实例代码详细讲解这两个新特性的使用方法和优势。
|
8天前
|
安全 Java API
RESTful API设计与实现:Java后台开发指南
【4月更文挑战第15天】本文介绍了如何使用Java开发RESTful API,重点是Spring Boot框架和Spring MVC。遵循无状态、统一接口、资源标识和JSON数据格式的设计原则,通过创建控制器处理HTTP请求,如示例中的用户管理操作。此外,文章还提及数据绑定、验证、异常处理和跨域支持。最后,提出了版本控制、安全性、文档测试以及限流和缓存的最佳实践,以确保API的稳定、安全和高效。
|
11天前
|
小程序 前端开发 API
小程序全栈开发中的RESTful API设计
【4月更文挑战第12天】本文探讨了小程序全栈开发中的RESTful API设计,旨在帮助开发者理解和掌握相关技术。RESTful API基于REST架构风格,利用HTTP协议进行数据交互,遵循URI、客户端-服务器架构、无状态通信、标准HTTP方法和资源表述等原则。在小程序开发中,通过资源建模、设计API接口、定义资源表述及实现接口,实现前后端高效分离,提升开发效率和代码质量。小程序前端利用微信API与后端交互,确保数据流通。掌握这些实践将优化小程序全栈开发。
|
20天前
|
前端开发 Java API
构建RESTful API:Java中的RESTful服务开发
【4月更文挑战第3天】本文介绍了在Java环境中构建RESTful API的重要性及方法。遵循REST原则,利用HTTP方法处理资源,实现CRUD操作。在Java中,常用框架如Spring MVC简化了RESTful服务开发,包括定义资源、设计表示层、实现CRUD、考虑安全性、文档和测试。通过Spring MVC示例展示了创建RESTful服务的步骤,强调了其在现代Web服务开发中的关键角色,有助于提升互操作性和用户体验。
构建RESTful API:Java中的RESTful服务开发
|
24天前
|
XML JSON 安全
谈谈你对RESTful API设计的理解和实践。
RESTful API是基于HTTP协议的接口设计,通过URI标识资源,利用GET、POST、PUT、DELETE等方法操作资源。设计注重无状态、一致性、分层、错误处理、版本控制、文档、安全和测试,确保易用、可扩展和安全。例如,`/users/{id}`用于用户管理,使用JSON或XML交换数据,提升系统互操作性和可维护性。
18 4
|
26天前
|
安全 API 开发者
构建高效可扩展的RESTful API服务
在数字化转型的浪潮中,构建一个高效、可扩展且易于维护的后端API服务是企业竞争力的关键。本文将深入探讨如何利用现代后端技术栈实现RESTful API服务的优化,包括代码结构设计、性能调优、安全性强化以及微服务架构的应用。我们将通过实践案例分析,揭示后端开发的最佳实践,帮助开发者提升系统的响应速度和处理能力,同时确保服务的高可用性和安全。
26 3
|
1月前
|
分布式计算 Java 程序员
Java 8新特性之Lambda表达式与Stream API
本文将详细介绍Java 8中的两个重要新特性:Lambda表达式和Stream API。Lambda表达式是Java 8中引入的一种简洁、匿名的函数表示方法,它允许我们将函数作为参数传递给其他方法。而Stream API则是一种新的数据处理方式,它允许我们以声明式的方式处理数据,从而提高代码的可读性和可维护性。通过本文的学习,你将能够掌握Lambda表达式和Stream API的基本用法,以及如何在项目中应用这两个新特性。
31 10
|
1月前
|
Java API 数据处理
Java 8新特性之Lambda表达式与Stream API
本文将介绍Java 8中的两个重要特性:Lambda表达式和Stream API。Lambda表达式是一种新的语法结构,允许我们将函数作为参数传递给方法。而Stream API则是一种处理数据的新方式,它允许我们对数据进行更简洁、更高效的操作。通过学习这两个特性,我们可以编写出更简洁、更易读的Java代码。
|
1月前
|
缓存 前端开发 API
构建高效可扩展的RESTful API:后端开发的最佳实践
【2月更文挑战第30天】 在现代Web应用和服务端架构中,RESTful API已成为连接前端与后端、实现服务间通信的重要接口。本文将探讨构建一个高效且可扩展的RESTful API的关键步骤和最佳实践,包括设计原则、性能优化、安全性考虑以及错误处理机制。通过这些实践,开发者可以确保API的健壮性、易用性和未来的可维护性。
|
1月前
|
API 开发者 UED
深入探讨RESTful API设计原则及最佳实践
在当今互联网时代,RESTful API已成为各种软件系统之间进行通信的重要方式。本文将从资源定义、URI设计、HTTP方法选择、状态码规范等方面深入探讨RESTful API设计的原则与最佳实践,帮助开发者更好地构建高效、健壮的API。