wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

Exception using Media.Query #280

Closed maxbale closed 2 years ago

maxbale commented 2 years ago

Hi, I'm using the following code to search a media by filename:

string stImageName ="myimage.jpg"; var queryBuilder = new MediaQueryBuilder(); queryBuilder.PerPage = 1; queryBuilder.Page = 1; queryBuilder.MediaType = MediaQueryType.Image; queryBuilder.Search = stImageName;

var queryresult = await wpClient.Media.Query(queryBuilder);

but I obtain the following exception:  "Parametro(i) non valido(i): after, before" Can someone help me? Thanks

WordPressPCL version: 1.9.0 WordPress version 5.8.2

navjot50 commented 2 years ago

I tried to reproduce your error and I didn't get any exception. Can you give the entire stack trace of the exception?

maxbale commented 2 years ago

Message: Parametro(i) non valido(i): after, before RequestData: {WordPressPCL.Models.BadRequest} RequestData.Data: {{ "status": 400, "params": { "after": "Data non valida. ", "before": "Data non valida. " }, "details": { "after": { "code": "rest_invalid_date", "message": "Data non valida. ", "data": null }, "before": { "code": "rest_invalid_date", "message": "Data non valida. ", "data": null } } }} RequestData.Name: rest_invalid_param StackTrace: in WordPressPCL.Utility.HttpHelper.d331.MoveNext() in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Runtime.CompilerServices.TaskAwaiter1.GetResult() in LctIntA2w.foInterface.d22.MoveNext() in C:\LineaComputers\wc_c47_lcta2w\SVILUPPO\LctSolA2w\LctIntA2w\foInterface.cs: riga 1327

Thank you

navjot50 commented 2 years ago

After and Before represent the parameters to limit the search for media items. Are you by chance setting the After and Before for MediaQueryBuilder? If not, then it may be possible that this might have happened due to Locale settings difference. Would you mind telling me what is CultureInfo.CurrentCulture in your program? Also, could you evaluate DateTime.MinValue.ToString("yyyy-MM-ddTHH:mm:ss") and tell what it gives as output? Also, if you add CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); at the start of the program does the problem go?

maxbale commented 2 years ago

Thank you very much! I didn't set Before and After parameters in the MediaQueryBuilder but my CultureInfo.CurrentCulture (it-IT) caused the exception. Setting CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); solved the trouble

navjot50 commented 2 years ago

This is a bug and I will issue a fix PR for this.