It Shows 405 Server Error at First Time to Get Result from API Server, and It Works Fine at Second Time

c# - 405 (Method Not Allowed) when POST from Angular 2 app

Your code seems ok.Try to change code as I have given below :

Angular

sendPtipo(delegacion: number,municipio: number,ejercicio: number,recinto: number,tipo: string){

let data = new Object();
data.delegacion = delegacion;
data.municipio = municipio;
data.ejercicio = ejercicio;
data.recinto = recinto;
data.tipo = tipo;

let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
let urlPtipo ='http://localhost:50790/ApiProductoTipo/CalculaPTRecinto';

return this._http.post(urlPtipo , data , options)
.map(data => {alert('ok');})
.catch(this.handleError);
}

private extractData(res: Response) {
let body = res.json();
return body.data || {};
}

private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}}

API in C#

using System.Collections.Generic;
using System.Web.Http;
using AppName.Models;
using AppName.Service;
using System.Linq;
using AppName.ViewModels;
using System.Net.Http;
using System.Net;
using System.Web.Http.Cors;

namespace Api.Services
{
// Allow CORS for all origins. (Caution!)
//[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ApiProductoTipoController : ApiController
{
public class myobj{
public int delegacion { get; set; }
public int municipio { get; set; }
public int ejercicio { get; set; }
public int recinto { get; set; }
public string tipo { get; set; }

}

private readonly IProductoTipoService productoTipoService;

public HttpResponseMessage Options()
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}

public ApiProductoTipoController(IProductoTipoService productoTipoService)
{
this.productoTipoService = productoTipoService;
}

[HttpPost]
[Route("~/ApiProductoTipo/CalculaPTRecinto")]
public HttpResponseMessage CalculaPTRecinto(myobj data)
{
var tipo = data.tipo;
...
}
}}

I was facing the same issue.The actual datatype of data you sre sending can't get at API side.That's why its giving 405 error.So try to send data as an object and also receive as an object at API side.

Hope this will help you

Parameters input from responseJSON to responseDecodable using Alamofire

I think you are mixing things up here. You are implementing Decodable and talking/naming it that way, but what you are doing is Encoding.

It seems the Api expects a simple String as paramter, constructing it with a [String: String] seems ok. But what you are trying to send is JSON. I think the request is failing while trying to encode: ["keys": "latlng,time,altitude"] to your custom struct. Hence the error:

urlRequestValidationFailed....bodyDataInGETRequest

As has allready been pointed out in the comments you would need to use your old constructor for sending the request.

AF.request(url_activity, method: .get, parameters: params, headers: head)

then use the method:

.responseDecodable(of: "what you expect in the response".self) {

Here you need to provide a custom struct that conforms to Decodable and represents the >"returning json"<


For the request you linked it would be:

struct ResponseElement: Codable {
let type: String
let data: [Double]
let seriesType: String
let originalSize: Int
let resolution: String

enum CodingKeys: String, CodingKey {
case type, data
case seriesType = "series_type"
case originalSize = "original_size"
case resolution
}
}

and decoding with:

.responseDecodable(of: [ResponseElement].self) {

HTTP Status 405 - Request method 'PUT' not supported

Try turning up the logging level for org.springframework.web to DEBUG. This will give you some insight into how Spring is trying to deal with the request. Hopefully, it will give you (or us) some more clues on how to fix it.

If you're using Spring Boot, just add this line to your application.properties file:

logging.level.org.springframework.web=DEBUG

Edit after seeing additional logging:

The 'PUT' not supported message is a bit misleading. The real problem comes before that. You don't have a valid CSRF token. How are you submitting the request? It looks like you are using the PostMan tool (but I am not familiar with this tool) rather than submitting the form directly from a web-page. There may be some way that you can add the token to your request using the tool. Does it work without the tool - submitting the form directly from the web-page?



Related Topics



Leave a reply



Submit