简体   繁体   中英

Retrofit Post request is not working and i'm getting 500 error code (Internal server erroe)

I have 500 internal server error, every time when I try to send POST request via Retrofit. When I sending GET request is working for me. Below code is in kotlin.

Code

 val token = (activity as MainActivity).stockService.status.getTocken()
        val client =
            OkHttpClient.Builder().addInterceptor { chain ->
                val newRequest: Request = chain.request().newBuilder()
                    .addHeader("Authorization", "Bearer $token")
                    .addHeader("Content-Type", "application/json")
                    .build()
                chain.proceed(newRequest)
            }.build()
        val gson = GsonBuilder()
            .setLenient()
            .create()
        val retrofit: Retrofit = Retrofit.Builder()
            .client(client)
            .baseUrl("https://example.com")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()

        val service = retrofit.create(RestClient::class.java)


        val paramObject = JSONObject()
        paramObject.put("disclosed_quantity", 0)
        paramObject.put("exchange", "abd")
        paramObject.put("instrument_token", 3045)
        paramObject.put("order_tag", "test_order")
        paramObject.put("order_type", "CHECK")


        val call = service.createUser(paramObject.toString())
        call.enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                Toast.makeText(activity, "Failed :$t", Toast.LENGTH_SHORT).show()
            }

            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                Toast.makeText(
                    activity,
                    "Sucess ${response.body()},${response.code()}, ${response.errorBody()}",
                    Toast.LENGTH_SHORT
                ).show()
            }

        })

RestClient

interface RestClient {

@POST("/api/v2/order")
fun createUser(@Body order: String): Call<Any> }

But when I try POST in the POSTMAN is working fine. I get response correctly I don't know where I am doing wrong.

Thank you in advance

It will be better not to create paramObject with JSONObject . It's much easier to delegate this job to Retrofit like this:

// Create Order object
class Order(
    val disclosed_quantity: Int,
    val exchange: String,
    val instrument_token: String,
    val order_tag: String,
    val order_type: String
)

// Use it as @Body
@POST("api/v2/order")
fun createUser(@Body order: Order): Call<Any>

The second problem may be in your URL at "/api/v2/order" . I think it should be "api/v2/order" without starting /

Base URL: always ends with /

@Url: DO NOT start with /

More details https://inthecheesefactory.com/blog/retrofit-2.0/en "New URL resolving concept"

you have tried http post request with retrofit, and server has responded to you with 500 error code, and you have tried http get request with browser and it have worked. Did you try http get request with retrofit? Did you check server logs? Are there any errors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM